//
//  LeftGroupViewController.m
//  Delicious2
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LeftGroupViewController.h"

@interface LeftGroupViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation LeftGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getData];
    [self creatCollectionView];
    self.navigationItem.rightBarButtonItem = nil;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
}

#pragma mark - creatSubviews

- (void)creatCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(WIDTH/ 3, (HEIGHT - 113) / 5);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    
    [_collectionView registerClass:[LeftGroupCustomCollectionViewCell class] forCellWithReuseIdentifier:@"LeftGroupCustomCollectionViewCellIdentifier"];
    
    
    self.collectionView.bounces = NO;
    [self.view addSubview:_collectionView];
    
}

#pragma mark -- collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LeftGroupCustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LeftGroupCustomCollectionViewCellIdentifier" forIndexPath:indexPath];
    //数组保护
    if (self.dataSource.count != 0) {
        LeftGroupModel *groupModel = self.dataSource[indexPath.item];
        cell.groupModel = groupModel;
    }
   
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //跳转
    LeftGroupModel *groupModel = self.dataSource[indexPath.item];
    LeftPandectViewController *pandectVC = [[LeftPandectViewController alloc] init];
    pandectVC.value = groupModel.id.intValue;
    pandectVC.strKind = self.strKind;
    pandectVC.navigationItem.title = groupModel.name;
    [self.navigationController pushViewController:pandectVC animated:YES];
}

#pragma mark - 获取数据
- (void)getData {
    
    NSDictionary *dic = @{@"kind" : self.strKind};
    
    [FSDNetWorkTool getWithURL:kClassifyGETURL withParameter:dic httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        
        NSArray *arr = result[@"categories"];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in arr) {
            LeftGroupModel *groupModel = [[LeftGroupModel alloc] initWithDictionary:dic];
            [array addObject:groupModel];
        }
        self.dataSource = [NSArray arrayWithArray:array];
        
        [self.collectionView reloadData];
        self.HUD.hidden = YES;
    } fail:^(NSError *error) {
        NSLog(@"错误");
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:3];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.collectionView nightMode];
    [self.collectionView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
